(function() {
	var ue;
	var oldDIVWidth;
	var userRight;
	/*$("#avatar").error(function() {
		$(this).attr("src", "../img/loading.gif");
		$(this).error = null;
	});
	$("#avatarbig").error(function() {
		$(this).attr("src", "../img/loading.gif");
		$(this).error = null;
	});*/
	/*$("#backloadingimg").on('click',function(){
		jQuery("#avatarbig").prop("src","");
	});*/
	$("body").on("click", ".extendimg", function (){
		jQuery("#avatarbig").prop("src","");
        var id=$(this).attr('id');
        if(id!="bimg"){
        	jQuery("#avatarbig").prop("src",$("#"+id+"_img").attr("src"));
        } else{
        	jQuery("#avatarbig").prop("src",$("#avatar").attr("src"));
        }
        
	});
	///游客登录
    jQuery('#subtnForVisitors').on('click', function () {
		    var name = "Visitors";
			var password ="visitors";
			var smsCode =jQuery("#smsCode").val();
			var identify = jQuery("#identify").val();
			var urladress =jQuery("#url").val();
				var url = window.rootPath + '/ui/login/msubmit';
				jQuery("input").attr("disabled", true);
				jQuery(".login").attr("disabled", true);
				jQuery(".set").attr("disabled", true);
				
				var data = {
					"accountName" : name,
					"password" : sha256_digest(password),
					"smsCode" : smsCode
				}
				jQuery.ajax({
					url : encodeURI(url),
					type : "post",
					dataType : "json",
					data : JSON.stringify(data),
					contentType : "application/json;charset=UTF-8",
					success : function(data) {
						jQuery("input").attr("disabled", false);
						jQuery(".login").attr("disabled", false);
						jQuery(".set").attr("disabled", false);
						if (data.code == 0) {
							jQuery('#error').html('用户名或密码错误');
							/*createCode();
							jQuery("#loginCode").val('');*/
						} else if (data.code == 1) {
							/*var picture = data.picture;
							localStorage.setItem('picture', picture);
							localStorage.setItem('name', name);
							localStorage.setItem('userId', data.userId);	*/	
							
							location.reload();
						}
						else if (data.code == 3) {
						
							/*message(data.message);*/
						}
						else if(data.code == 99){//验证码失败
							/*jQuery('#error').html(data.message);*/
						}
					}
				});
    });
	///用户登录
    jQuery('#subtnForUser').on('click', function () {
		    var name = jQuery("#user").val();
			var password =jQuery("#pwd").val();
			var smsCode =jQuery("#smsCode").val();
			var identify = jQuery("#identify").val();
			var urladress =jQuery("#url").val();
				var url = window.rootPath + '/ui/login/msubmit';
				jQuery("input").attr("disabled", true);
				jQuery(".login").attr("disabled", true);
				jQuery(".set").attr("disabled", true);
				
				
				$("#popupWait").popup();
        		$("#popupWait").popup('open');
				/*jQuery('#loadingImage').show();*/
				var data = {
					"accountName" : name,
					"password" : sha256_digest(password),
					"smsCode" : smsCode
				}
				jQuery.ajax({
					url : encodeURI(url),
					type : "post",
					dataType : "json",
					data : JSON.stringify(data),
					contentType : "application/json;charset=UTF-8",
					success : function(data) {
						jQuery("input").attr("disabled", false);
						jQuery(".login").attr("disabled", false);
						jQuery(".set").attr("disabled", false);
						/*jQuery('#loadingImage').hide();*/
						$("#popupWait").popup('close');
						if (data.code == 0) {
							jQuery('#error').html('用户名或密码错误');
							/*createCode();
							jQuery("#loginCode").val('');*/
						} else if (data.code == 1) {
							/*var picture = data.picture;
							localStorage.setItem('picture', picture);
							localStorage.setItem('name', name);
							localStorage.setItem('userId', data.userId);	*/	
							
							location.reload();
						}
						else if (data.code == 3) {
						
							/*message(data.message);*/
						}
						else if(data.code == 99){//验证码失败
							/*jQuery('#error').html(data.message);*/
						}
					}
				});
    });
$("body").on("click", ".menu-scd-click", function (){
	        var id=$(this).attr('id');
	        var s=$(this).attr("href");
	        $("#menu-scd").popup("close");
	        if(id=="goTop"){
	        	$('html,body').animate({scrollTop: '0px'}, 1000);return false;
	        }else if(id=="goBottom"){
	        	$('html,body').animate({scrollTop:$(document).height()+'px'},1000);return false;
	        }else if(id=="goaAnds"){
	        	$('html,body').animate({scrollTop: '0px'}, 1000);
	        }
		});
	$("body").on("click", ".menuClick", function (){
		$("#menu-scd-list").empty();
		/*alert($(window).height()); //浏览器当前窗口可视区域高度 
		alert($(document).height()); //浏览器当前窗口文档的高度 
		alert($(document.body).height());//浏览器当前窗口文档body的高度 
		alert($(document.body).outerHeight(true));*/
		var id=$(this).attr("id");
		if(id=="menu-login"){
			$("#popupLoginSelect").popup("open");
		}else{
			if(id=="menu-home"){
				var contenthtml='<li class="ui-li-icon ui-corner-none" data-icon="false"><a href="#sPanel" class="menu-scd-click" id="goaAnds">投诉建议</a></li>'
				+'<li class="ui-li-icon ui-corner-none" data-icon="false"><a href="#" class="menu-scd-click" id="goTop">返回顶部</a></li>'
				+'<li class="ui-li-icon ui-corner-none" data-icon="false"><a href="#" class="menu-scd-click" id="goBottom">返回底部</a></li>';
				$("#menu-scd-list").append(contenthtml);
				$("#menu-scd-list").listview('refresh');
				}else{
					var contenthtml="";
					
					for(var i=0;i<userRight.length;i++){
						if(userRight[i].mobile!=undefined&&userRight[i].mobile=="true"&&userRight[i].target!="#"&&userRight[i].parentId==id){
							var url='javascript: ;';
							var isGo=false;
							var menuType="menuClick";
							var icon="home";
							var dataajax ='';
							if(userRight[i].icon!=undefined&&userRight[i].icon!=""){
								icon=userRight[i].icon;
							}
						    url=window.rootPath+userRight[i].target+"?device=mobile&identify="+$("#identify").val();
						    isGo=true;
						    menuType="menu-scd-click-bak";
						    dataajax ='data-ajax="false"';
							    
						 contenthtml +='<li class="ui-li-icon ui-corner-none" data-icon="false"><a href="'+url+'" data-icon="'+icon+'" class="'+menuType+'" id="'+userRight[i].id+'" '+dataajax+'>'+userRight[i].moduleName+'</a></li>';
						}
					}
					if(contenthtml!=""){
						$("#menu-scd-list").append(contenthtml);
						$("#menu-scd-list").listview('refresh');
					}
				}
				
				/*var id=$(this).attr("id");*/
				openGroupMenu(id,"menu-scd","menu-scd-arrow");
		}
		});
	/*计算菜单位置*/
	function openGroupMenu(controller,controllerscd,controllerscdarrow){
	    var left, top;
	    var rect = document.getElementById(controller).getBoundingClientRect();//controller.getBoundingClientRect();
	    var winrect =document.getElementById(controllerscd).getBoundingClientRect();//controllerscd.getBoundingClientRect();
	    left = rect.left+ winrect.width/2;
	    top = rect.top-winrect.height/2+$(document).scrollTop();
	    $("#"+controllerscd).popup("open", {
	                x:left,
	                y:top});
	    /*计算三角型的位置*/
	    winrect =document.getElementById(controllerscd).getBoundingClientRect();
	    document.getElementById(controllerscdarrow).style.left = (rect.left + rect.width/2 - winrect.left) + "px";
	};
	
	$(document).ready(function() {
		
		/*
		 * ue = UE.getEditor('editor',{ toolbars: [] ,elementPathEnabled : false
		 * //是否启用元素路径，默认是true显示 ,wordCount:false //是否开启字数统计 ,readonly:true });
		 */
		/*var edit = $("#displayEdit").val();
		var check = $("#displayCheck").val();
		var inspection=$("#displayInspection").val();
		var kitchen=$("#displayKitchen").val();
		
		$('#myedit').css('display', edit);
		$('#myinspection').css('display', inspection);
		$('#mycheck').css('display', check);
		$('#mykitchen').css('display', kitchen);*/
		
		$('#stardivT').raty();
		$('#stardivT').raty({
			number : 5,
			score : 0,
			size:$("#myTableT").attr("width")-5,
			cancel : true
		});
		getData();
		initBussinessContent();
		// 初始化巡检历史记录
		initPartInspections();
		// 初始化举报信息
		initPartAccusations();
	});
	/*// 编辑商户信息
	jQuery('#myedit').on(
			'click',
			function() {
				window.location = window.rootPath
						+ "/ui/bussinessEdit?device=mobile&&identify="
						+ $("#identify").val();
			});
	// 核检商户信息
	jQuery('#mycheck').on(
			'click',
			function() {
				window.location = window.rootPath
						+ "/ui/bussinessCheck?device=mobile&&identify="
						+ $("#identify").val();
			});
	// 巡检商户信息 
	jQuery('#mykitchen').on(
			'click',
			function() {
				window.location = window.rootPath
						+ "/ui/kitchenwaste?device=mobile&&identify="
						+ $("#identify").val();
			});
	// 巡检商户信息
	jQuery('#myinspection').on(
			'click',
			function() {
				window.location = window.rootPath
						+ "/ui/bussinessInspection?device=mobile&&identify="
						+ $("#identify").val();
			});*/
	// /编辑
	jQuery('#saveBtnT').on(
			'click',
			function() {
				var temp = $('#stardivT').raty('score');
				if (temp == undefined) {
					temp = 0;
				}
				var url = window.rootPath
						+ '/ui/bussinessmain/saveBusinessAccusationWithFile';
			var	d = {
					"accusationType" : $("#accusationType").val(),
					"accusationScale" : temp * 20,
					"accusationReason" : $("#accusationReason").val(),
					"businesssellerBaseInfo" : $("#identify").val()
					
				};
				$.ajaxFileUpload({
					url : url,
					secureuri : false,// 是否需要安全协议
					fileElementId : 'file_upload',// 文件上传的Id属性
					dataType : 'JSON',
					data : d,
					success : function(result, status) {
						getData();
					}
				});

				/*jQuery.ajax({
					type : 'post',
					url : encodeURI(url),
					data : JSON.stringify(d),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function(data) {
						if (data.code == -1) {
							 message(data.message); 
						} else {
							
							 * $("#popupView").popup();
							 * $("#popupView").popup('open');
							 
							 $("#test").trigger("click"); 
						}
						getData();
					}
				});*/

			});

	function initPartAccusations() {
		var identify = $("#identify").val();

		$.ajax({
			url : window.rootPath + '/ui/bussinessmain/listPartAccusations/'
					+ identify,
			dataType : 'json',
			type : 'get',
			success : function(result) {

				var accusations = result.data.accusations;

				if (result.code == 1) {

					buildAccusations(accusations, "#accusations");

				}
			}
		});
	}
	;

	function buildAccusations(info, str) {

		if (info.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无考核信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}

		for (var i = 0; i < info.length; i++) {

			var purl = info[i].pictureAddr;// 没有上传图片

			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = rootPath + "/" + purl;
			
			
			
			var contenthtml ="<li><table><tr>" +
						'<td width="30%"><a href="#pop" data-rel="popup" data-position-to="window" id="'+info[i].id+'" class="extendimg" data-transition="fade"><img id="'+info[i].id+'_img" width="100%" src="'+purl+'"/></a></td><td>' 
			            +"<font >举报类型:&nbsp;"+ info[i].accusationType.title + "</font><br/>" +
							"<font >举报原因:&nbsp;"+ info[i].accusationReason + "</font><br/>" +
							"<font>满意度:&nbsp;"+ info[i].accusationScale + "</font><br/>" +
							"<font>举报人:&nbsp;"+info[i].user.name+"</font><br/>"+
							"<font>举报人电话:&nbsp;"+info[i].user.contactType+"</font><br/>"+
							"<font>时间:&nbsp;"+ moment(info[i].accusationDate).format('YYYY-MM-DD HH:mm')+"</font>" +
						"</td></tr></table></li>";

			$(str).append(contenthtml);

		}
		$(str).listview('refresh');
	}
	;
	function initPartInspections() {
		var identify = $("#identify").val();

		$.ajax({
			url : window.rootPath + '/ui/bussinessmain/listPartInspections/'
					+ identify,
			dataType : 'json',
			type : 'get',
			success : function(result) {

				var inspections = result.data.inspections;

				if (result.code == 1) {

					buildInspections(inspections, "#inspections");

				}
			}
		});
	}
	;
	function buildInspections(info, str) {

		if (info.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无巡检信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}

		for (var i = 0; i < info.length; i++) {

			var purl = info[i].pictureAddr;// 没有上传图片

			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = rootPath + "/" + purl;
			var title = info[i].inspectCondition.title;

			var co = '';
			if (title == "不及格") {
				co = 'red';
			} else {
				co = 'blue';
			}
			var contenthtml = "" + "<li>" + "<table>" + "<tr>"
					+ '<td width="30%"><a href="#pop" data-rel="popup" data-position-to="window" id="'+info[i].id+'" class="extendimg" data-transition="fade"><img id="'+info[i].id+'_img"width="100%" src=' + purl
					+ "/></a></td>" + "<td>" + "<font color=" + co + ">总体评价:&nbsp;"
					+ title + "</font><br/>" + "<font>巡检人:&nbsp;"
					+ info[i].user.name + "</font><br/>" + "<font>备注:&nbsp;"
					+ info[i].remark + "</font><br/>" +

					"<font>时间:&nbsp;"
					+ moment(info[i].inspectDate).format('YYYY-MM-DD HH:mm')
					+ "</font>" + "</td>" + "</tr>" + "</table>" + "</li>";

			$(str).append(contenthtml);

		}
		$(str).listview('refresh');
	}
	;
	function getData() {
		var identify = $("#identify").val();

		var url = window.rootPath + '/ui/bussinessmain/getBussinesserInfo/'
				+ identify;
		jQuery
				.ajax({
					url : encodeURI(url),
					cache : false,
					type : "get",
					data : "",
					dataType : "json",
					contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
					success : function(data) {
						//#动态添加菜单
						var contenthtml='<ul id="menu-list">'
						    +'<li><a href="javascript: ;" data-icon="home" class="menuClick"'
							+'id="menu-home">首页</a></li>'
							+'<li><a href="javascript: ;" data-icon="home" class="menuClick"'
							+'id="menu-login">登录</a></li>';
						userRight =data.modules;
						for(var i=0;i<data.modules.length;i++){
							if(data.modules[i].mobile!=undefined&&data.modules[i].mobile=="true"&&data.modules[i].menuLevel==1){//&&data.modules[i].menuLevel==1
								var url='javascript: ;';
								var isGo=false;
								var menuType="menuClick";
								var icon="home";
								var dataajax ='';
								if(data.modules[i].icon!=undefined&&data.modules[i].icon!=""){
									icon=data.modules[i].icon;
								}
								
								if(data.modules[i].target!="#"){
								    url=window.rootPath+data.modules[i].target+"?device=mobile&identify="+$("#identify").val();
								    isGo=true;
								    menuType="menu-scd-click-bak";
								    dataajax ='data-ajax="false"';
								}
								 contenthtml +='<li><a href="'+url+'" data-icon="'+icon+'" class="'+menuType+'" id="'+data.modules[i].id+'" '+dataajax+'>'+data.modules[i].moduleName+'</a></li>';
							}
						}
						 contenthtml+='</ul>';
						 $('#menu-bar').empty();
						 $('#menu-bar').append(contenthtml);
						 $('#menu-bar').navbar('refresh');
						/*$("#menu-list").append(contenthtml);*/
						/*$("#menu-list").listview('refresh');*/
						$('#stardiv').raty();
						if (data.evaluate != undefined) {
							$("#evaluateScale").html(
									data.evaluate.evaluateScale + "分");
							$('#stardiv').raty({
								number : 5,
								readOnly : true,
								size:$("#btitle").attr("width")-5,
								score : data.evaluate.evaluateScale / 20.0
							});// evaluateLevel
						} else {
							$("#evaluateScale").html("-- 分");
							$('#stardiv').raty({
								number : 5,
								size:$("#btitle").attr("width")-5,
								readOnly : true,
								score : 0
							});
						}
						if (data.businesssellerBaseInfo != undefined) {
							var avatarUrl=data.businesssellerBaseInfo.pictureAdress;
							if(avatarUrl==undefined||avatarUrl==""){
								avatarUrl="../img/nopicture.png";
							}else{
								avatarUrl=window.rootPath+'/'+avatarUrl;
							}
							/*var avatarUrl = window.rootPath + '/a/' + 'avatar/'
									+ data.businesssellerBaseInfo.pictureAdress;*/
							/* jQuery("#avatar").prop("src","#"); */
							jQuery("#avatar").prop("src",
									avatarUrl + '?t=' + Math.random());
							/* jQuery("#avatarbig").prop("src","#"); */
							jQuery("#avatarbig").prop("src",
									avatarUrl + '?t=' + Math.random());

							$("#maintitle").html(
									data.businesssellerBaseInfo.shopName);
							$("#shopName").html(
									data.businesssellerBaseInfo.shopName);
							$("#shopNameT").html(
									data.businesssellerBaseInfo.shopName);
							if (data.businesssellerBaseInfo.infoState != undefined) {
								/* $("#infoState").html(data.businesssellerBaseInfo.infoState.title); */
								var path = "";
								if (data.businesssellerBaseInfo.infoState.title == '待认证') {
									path = "../img/authentication_in.png";
								} else if (data.businesssellerBaseInfo.infoState.title == '认证拒绝') {
									path = "../img/authentication_notaccess.png";
								} else if (data.businesssellerBaseInfo.infoState.title == '认证通过') {
									path = "../img/authentication_access.png";
								} else if (data.businesssellerBaseInfo.infoState.title == '初始化') {
									path = "../img/authentication_init.png";
								} else if (data.businesssellerBaseInfo.infoState.title == null) {
									path = "";
								}
								$("#infoState").attr('src', path);
							}

							$("#shopAdress").html(
									data.businesssellerBaseInfo.shopAdress);
							$("#areaMeasure").html(
									data.businesssellerBaseInfo.areaMeasure);
							$("#signPerson").html(
									data.businesssellerBaseInfo.signPerson);
							$("#managementRange")
									.html(
											data.businesssellerBaseInfo.managementRange);
							$("#memo").html(data.businesssellerBaseInfo.memo);
							if (data.businesssellerBaseInfo.areaInfo != undefined) {
								$('#area')
										.html(
												"管理单位:"
														+ data.businesssellerBaseInfo.areaInfo.title);
								$('#areaphone')
										.html(
												"联系电话:"
														+ data.businesssellerBaseInfo.areaInfo.value);
							}
							if (data.businesssellerBaseInfo.arreaManagementPerson) {
								$('#areaMperson')
										.html(
												data.businesssellerBaseInfo.arreaManagementPerson.name);
							}
						}

						// $("#area").find("option:contains('"+data.areaInfo.title+"')").attr("selected",true);
						// jQuery('#area').val(data.areaInfo.id);
						// $("#areaMperson").find("option:contains('"+data.arreaManagementPerson.name+"')").attr("selected",true);
						// jQuery('#areaMperson').val(data.arreaManagementPerson.id);

						/*
						 * $("#managementPerson").html("管理人员:
						 * "+data.managementPerson.name);
						 */
					}
				})
	}
	;
	$("#file_upload").change(function () {
	    var $file = $(this);
	    var fileObj = $file[0];
	    var windowURL = window.URL || window.webkitURL;
	    var dataURL;
	    var $img = $("#avatart");

	    if (fileObj && fileObj.files && fileObj.files[0]) {
	        dataURL = windowURL.createObjectURL(fileObj.files[0]);
	        $img.attr('src', dataURL);
	    } else {
	        dataURL = $file.val();
	        var imgObj = document.getElementById("preview");
	        // 两个坑:
	        // 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
	        // 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
	        imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
	        imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

	    }
	});
	jQuery('#fileclick').on('click', function () {
		return  $("#file_upload").click();
	});
	function initBussinessContent() {
		var divBussinessContent = jQuery("#bussinessContent");
		var identify = jQuery("#identify").val();
		divBussinessContent.empty();
		if ("" != identify) {
			var url = window.rootPath
					+ '/ui/bussinessmain/getBussinesserContent/' + identify;
			jQuery
					.ajax({
						type : 'get',
						url : url,
						data : "",
						dataType : 'json',
						contentType : 'application/json;charset=utf-8',
						success : function(data) {
							if (data.code == 1) {
								var bussinessContent = data.data.BussinessContent.bussinessContent;
								divBussinessContent.append(bussinessContent);
								// var tabContent = jQuery("#contenttab");
								// tabContent.table('refresh');

								var divWidth = divBussinessContent.width();
								oldDIVWidth = divWidth;
								var scale = divWidth / 1000;
								jQuery("#bussinessContent table").removeAttr(
										"width");
								jQuery("#bussinessContent tr").removeAttr(
										"width");
								jQuery("#bussinessContent th").removeAttr(
										"width");
								jQuery("#bussinessContent td").removeAttr(
										"width");
								// 缩放
								jQuery("#bussinessContent img")
										.each(
												function() {
													var oWidth = $(this)
															.width();
													var oHeight = $(this)
															.height();
													if (oHeight != 0) {
														$(this).width(
																oWidth * scale);
														$(this)
																.height(
																		oHeight
																				* scale);
													} else {
														$(this).width(
																divWidth * 0.5);
													}
													$(this)
															.prop("href",
																	"#bussinessContentPop");
													$(this).prop("data-rel",
															"popup");
													$(this).prop(
															"data-position-to",
															"window");
													$(this).prop(
															"data-transition",
															"fade");
													$(this)
															.bind(
																	"click",
																	function() {
																		jQuery(
																				"#imgbig")
																				.prop(
																						"src",
																						this.src);
																		// jQuery("#bussinessContentPop").popup("open");
																	});
												})

								/*
								 * setTimeout(function(){
								 * ue.setContent(bussinessContent,false);
								 * },666);
								 */
							}
						}
					});
		}
	}
	;

	// 监听窗体变化
	$(window).resize(function() {
		var divBussinessContent = jQuery("#bussinessContent");
		var divWidth = divBussinessContent.width();
		var scale = divWidth / oldDIVWidth;
		oldDIVWidth = divWidth;
		// 缩放
		jQuery("#bussinessContent img").each(function() {
			var oWidth = $(this).width();
			var oHeight = $(this).height();
			if (oWidth != 50 || oHeight != 0) {
				$(this).width(oWidth * scale);
				$(this).height(oHeight * scale);
			}
		})
	});
})();